package com.cqeec.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@R
//表示以下都带@Controller @ResponseBody
@RequestMapping(value = "/api")
// http://localhost:8080/api
public class MyController{
    /**
    * 默认采用的是请求转发
    * 重定向
    * @return 定义返回的是一个逻辑视图(页面)名称
    * */
    @RequestMapping(value = "/hello")
    public String Hello(HttpServletRequest request){  //  /api/hello
        request.setAttribute("aa","ffff");
        request.getSession().setAttribute("bb","abcc");
        return "hello";     //  前缀 + 逻辑视图名称 +后缀    最终格式:/hello.jsp
    }
    /* 处理器Handler */
    @RequestMapping(value = "/test")
    public String Test(HttpServletRequest request){   //  /api/test
        request.setAttribute("aa","ffff");
        request.getSession().setAttribute("bb","abcc");
        return "redirect:/test.jsp";
    }

    /**
    *   根据映射路径生成物理视图名称  /api/a.jsp
    * */
    @RequestMapping(value = "/a")
    public void Abc(HttpServletRequest request){
        System.out.println("--------------------------------");
    }

    /**
    *   ajax请求
    *   postman
    * */
    @RequestMapping(value ="/ajax")
    //给api加上@RestController后可以省去@ResponseBody
    //@ResponseBody
    public Map<String,Object> getXXXData(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        result.put("a",999999);
        result.put("b","这是帅哥的json");
        result.put("c",new Date());
        result.put("d","我是帅哥！");
        return result;//转换成json数据(内置json转换器 jackon)
    }
}
